package cn.qingsec.controller;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.context.model.SaStorage;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.convert.Convert;
import cn.qingsec.utils.LoginHelper;
import cn.qingsec.utils.LoginUser;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping
public class IndexController {
    public static final String LOGIN_USER_KEY = "loginUser";
    public static final String USER_KEY = "userId";
    // 登录接口
    @PostMapping("login")
    public SaResult doLogin(String username,String password) {
        SaStorage storage = SaHolder.getStorage();
        LoginUser loginUser = new LoginUser();
        loginUser.setPassword(password);
        loginUser.setUserType("2");
        // 第1步，先登录上
        if("admin".equals(username)){
            StpUtil.login(1);
            loginUser.setUserId(1L);
            loginUser.setMenuPermission(
                    Stream.of("user.list","user.add","user.update","user.delete").collect(Collectors.toSet()));
        }else {
            StpUtil.login(2);
            loginUser.setUserId(2L);
            loginUser.setMenuPermission(
                    Stream.of("role.list","role.add","role.update","role.delete").collect(Collectors.toSet()));
        }
        storage.set(LOGIN_USER_KEY, loginUser);
        storage.set(USER_KEY, loginUser.getUserId());
        StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser);
        // 第2步，获取 Token  相关参数
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        // 第3步，返回给前端
        return SaResult.data(tokenInfo);
    }
    // 查询登录状态
    @GetMapping("isLogin")
    public SaResult isLogin() {
        return SaResult.ok("是否登录：" + StpUtil.isLogin());
    }
    // 打印当前token信息，
    @GetMapping("token")
    public SaResult tokenInfo() {
        System.out.println("======================= 进入方法，打印当前token信息 ========================= ");
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        System.out.println(tokenInfo);
        return SaResult.data(tokenInfo);
    }
    // 测试注销
    @GetMapping("logout")
    public SaResult logout() {
        StpUtil.logout();
        return SaResult.ok();
    }

    @GetMapping("getLoginUser")
    public SaResult getLoginUser() {
        LoginUser l1 = (LoginUser) SaHolder.getStorage().get(LOGIN_USER_KEY);
        System.out.println();
        LoginUser l2 = (LoginUser) StpUtil.getTokenSession().get(LOGIN_USER_KEY);
        Long u1 = Convert.toLong(SaHolder.getStorage().get(USER_KEY));
        Long u2 = Convert.toLong(StpUtil.getExtra(USER_KEY));
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        LoginUser l3 = LoginHelper.getLoginUser(tokenInfo.tokenValue);
        Map<String,Object> map = new HashMap<>();
        map.put("l1",l1);
        map.put("l2",l2);
        map.put("l3",l3);
        map.put("u1",u1);
        map.put("u2",u2);
        return SaResult.data(map);
    }
}
